<template>
	<view>
		<view class="form-box">
			<u-form ref="uForm" label-width="130rpx" :model="form">
				<u-form-item label="满意度" prop="star" required>
					<u-rate count="5" v-model="form.star"></u-rate>
				</u-form-item>
				<u-form-item label="评价内容" prop="content">
					<u-textarea v-model="form.content" clearable placeholder="亲,您对这个订单满意吗？您的评价有助于我们提升服务质量哦～"></u-textarea>
				</u-form-item>
				<u-form-item label="添加图片" prop="urls">
					<u-upload :fileList="form.fileList" @afterRead="afterRead" @delete="deletePic" accept="image" multiple :maxCount="5" :previewFullImage="true">
					</u-upload>
				</u-form-item>
			</u-form>
		</view>
		<view class="submit-btn">
			<u-button type="success" @click="submit">提交评价</u-button>
		</view>
	</view>
</template>

<script>
	const PAY = require('@/common/pay.js')
	export default {
		data() {
			return {
				rules: {
					name: [{
						star: true,
						message: '不能为空',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
				},
				form: {
					orderId: 0,
					star: 0,
					content: null,
					fileList: [],
				},
			}
		},
		created() {

		},
		mounted() {

		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(e) {
			if(e.id) {
				this.form.orderId=e.id;
				// this._detail(e.id)
			}
		},
		onShow() {

		},
		methods: {
			async _detail(id) {
				const res = await this.$wxapi.orderDetail(this.token, id)
				if(res.code == 0) {
					// this.form = Object.assign(this.form, res.data)
				}
			},
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				this.form.fileList = this.form.fileList.concat(event.file)
			},
			deletePic(event) {
				this.form.fileList.splice(event.index, 1)
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					this._submit()
				}).catch(errors => {
					uni.showToast({
						title: '表单请填写完整',
						icon: 'none'
					})
				})
			},
			async _submit() {
				let upUrls = []
				// 批量上传附件
				if (this.form.fileList) {
					for (let index = 0; index < this.form.fileList.length; index++) {
						const pic = this.form.fileList[index];
						const res = await this.$wxapi.uploadFile2(this.token, pic.url)
						if (res.code == 0) {
							upUrls.push(res.data.url)
						}
					}
				}
				const res = await this.$wxapi.addComment({
					token: this.token,
					userId: getApp().globalData.userId,
					orderId: this.form.orderId,
					star: this.form.star,
					content: this.form.content,
					urls: upUrls
				})
				if (res.code == 0) {
					uni.showModal({
						showCancel: false,
						title: '成功',
						content: '提交成功',
						confirmText: '知道了',
						success: () => {
							uni.navigateBack()
						}
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
		}
	}
</script>
<style scoped lang="scss">
	.form-box {
		padding: 32rpx;
	}
</style>
